package ca.jacobk.fileio;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

import android.content.Context;

public class OptionsManager {

	List<WheelOption> optionsList;
	Random randNum;
	static OptionsManager instance = null;
	public static Context context;
	
    public static final int IDX_NAME = 0;
    public static final int IDX_PRICEMIN = 1;
    public static final int IDX_PRICEMAX = 2;
    public static final int IDX_LOCATION = 3;
	
	private OptionsManager(){
		
		optionsList = new ArrayList<WheelOption>();
		randNum = new Random();
	}
	
	//Singleton Pattern
	public static OptionsManager getInstance(){
		
		if(instance == null){
			instance = new OptionsManager();
			
			//Debugging List
			WheelOption item1 = new WheelOption("KFC", 5, 10, null);
			WheelOption item2 = new WheelOption("The Keg", 15, 20, "Somewhere");
			WheelOption item3 = new WheelOption("Subway", 7, 14, "Texas");
			WheelOption item4 = new WheelOption("Binos", 2, 8, "Good ole Kingsway");
			instance.optionsList.add(item1);
			instance.optionsList.add(item2);
			instance.optionsList.add(item3);
			instance.optionsList.add(item4);
			
			try{
				instance.save();
				instance.optionsList.clear();
				instance.load();
			} catch (IOException e){
				System.out.print(e);
			}
		}
		
		return instance;
	}
	
	public void setContext(Context cont){
		context = cont;
	}
	
	//Returns the whole list of options.
	public List<WheelOption> getAllOptions(){
		
		return optionsList;
	}
	
	//Creates a new WheelOption object and adds it to the list.
	public WheelOption addOption(String name, int min, int max, String location){
		
		WheelOption temp = new WheelOption(name, min, max, location);
		optionsList.add(temp);
		
		return temp;
	}
	
	//Selects the Winning option
	public WheelOption chooseWinningOption(){
		
		int chosenNum = 0;
		
		chosenNum = randNum.nextInt(optionsList.size());
		
		return optionsList.get(chosenNum);
	}
	
	//Removes the WheelOption from the List
	public Boolean removeOption(WheelOption option){
	
		optionsList.remove(option);
		
		return true;
	}
	
	//Saves the current list to file.
	public void save() throws IOException{
		
		String filename = "wheel_data.txt";
        FileOutputStream out;
       
        try {
        		out = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
               
                for (WheelOption wo: optionsList)
                {
                        String temp = wo.name + "," + wo.priceRangeMin + "," + wo.priceRangeMax + "," + wo.location + "\n";
                        out.write(temp.getBytes());
                }
                        out.close();
        }
        catch (Exception e)
        { System.out.println ("Error writing to file"); }
	}
	
	//Loads the list from file.
	public void load() throws IOException{
		
        FileInputStream fstream = context.openFileInput("wheel_data.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        String wheelOptionText[];
       
        while ((strLine = br.readLine()) != null)
        {
                wheelOptionText = strLine.split(",");
                addOption(wheelOptionText[IDX_NAME],
                          Integer.parseInt(wheelOptionText[IDX_PRICEMIN]),
                          Integer.parseInt(wheelOptionText[IDX_PRICEMAX]),
                          wheelOptionText[IDX_LOCATION]
                );
        }

        in.close();
	}
}
